import version from './version'
import * as echarts from 'echarts'
import {  grid, areaStyle, legend, xAxisName, yAxisName, tooltip } from '../libs/index.js'

let name = 'data-echarts'

const Echarts = class {
	constructor(dom, options) {
		if (!dom) throw new Error('必须传入一个 DOM 容器')
		this.dom = dom
		options.xAxis = xAxisName(options.xAxis)
		options.yAxis = yAxisName(options.yAxis)
		options.grid = grid(options.grid)
		options.tooltip = tooltip()
		this.options = options
		this.instance = echarts.init(dom)
	}
	// 处理series
	series(){
		const options = this.options
		options.series = options.series.map(s => ({
			name: s.name,
			type: s.extra?.type || options.type,  // 局部类型优先，否则用全局 type
			data: s.data,
			...s.extra
		}))
	}
	handleXAxis(len = 2, ellipsis = '...') {
		this.options.xAxis.data = this.options.xAxis.data.map((item, index) => {
			if (item.length > len) {
				item = item.slice(0, len) + ellipsis
			}
			return item
		})
	}
	setOption() {
		this.handleXAxis(1, '~~')
		// this.series()
		console.log('options', this.options);
		this.instance.setOption(this.options)
	}
}
const obj = {
	name,
	version,
	Echarts
}
export default obj
export { name, version, Echarts }
